package com.lanou.projecta_163news.Activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.lanou.projecta_163news.base.BaseActivity;
import com.lanou.projecta_163news.fragment.MainFragment;
import com.lanou.projecta_163news.fragment.PersonFragment;
import com.lanou.projecta_163news.fragment.ReadFragment;
import com.lanou.projecta_163news.fragment.TopicFragment;
import com.lanou.projecta_163news.adapter.MainAdapter.MainTabLayoutAdapter;
import com.lanou.projecta_163news.R;
import com.lanou.projecta_163news.fragment.VideoFragment;

import java.util.ArrayList;

public class MainActivity extends BaseActivity {
    private Toolbar mainToolbar;
    private ViewPager viewPager;
    private TabLayout tabLayout;
    private ArrayList<Fragment> mainData;
    private MainTabLayoutAdapter tabLayoutAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    @Override
    protected void initView() {
        viewPager = bindView(R.id.activity_main_viewpager);
        tabLayout = bindView(R.id.activity_main_tablayout);
        mainToolbar = bindView(R.id.main_toolbar);
        mainToolbar.setTitle("核聚News");
        setSupportActionBar(mainToolbar);

    }

    @Override
    protected void initData() {
        inViewpager();
    }


    @Override
    protected int getLayout() {
        return R.layout.activity_main;

    }

    private void inViewpager() {

//        viewPager = (ViewPager) findViewById(R.id.activity_main_viewpager);
//        tabLayout = (TabLayout) findViewById(R.id.activity_main_tablayout);
        mainData = new ArrayList<>();
        mainData.add(new MainFragment());
        mainData.add(new ReadFragment());
        mainData.add(new VideoFragment());
        mainData.add(new TopicFragment());
        mainData.add(new PersonFragment());
        tabLayoutAdapter = new MainTabLayoutAdapter(getSupportFragmentManager(), mainData);
        viewPager.setAdapter(tabLayoutAdapter);
        tabLayout.setTabTextColors(Color.BLACK, Color.GRAY);
        tabLayout.setSelectedTabIndicatorColor(Color.YELLOW);
        tabLayout.setupWithViewPager(viewPager);


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_toolbar_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.main_toolbar_news:
                Toast.makeText(MainActivity.this, "新闻", Toast.LENGTH_SHORT).show();
                break;
            case R.id.main_toolbar_person:
                Toast.makeText(MainActivity.this, "个人", Toast.LENGTH_SHORT).show();
                break;
            case R.id.main_toolbar_setting:
                Toast.makeText(MainActivity.this, "设置", Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onOptionsItemSelected(item);
    }


}
